﻿namespace YK.Console.Business.RoleMenuRoutes;

internal class SaveRoleMenuRouteHandler(IRepository<RoleMenuRoute> _repo) : IRequestHandler<SaveRoleMenuRouteRequest, bool>
{
    public async Task<bool> Handle(SaveRoleMenuRouteRequest request, CancellationToken cancellationToken)
    {
        await _repo.DeleteAsync(x => x.RoleId == request.RoleId, cancellationToken);

        if (request.MenuRouteIds.Any())
        {
            var roleMenuRoutes = request.MenuRouteIds.Select(x => new RoleMenuRoute
            {
                MenuRouteId = x,
                RoleId = request.RoleId
            }).ToList();

            await _repo.AddRangeAsync(roleMenuRoutes, cancellationToken);
        }

        return true;
    }
}
